package org.jeecg.modules.demo.stock.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.JstlHttpUpload;
import org.jeecg.modules.demo.stock.entity.TbStockK;
import org.jeecg.modules.demo.stock.mapper.TbStockKMapper;
import org.jeecg.modules.demo.stock.service.ITbStockKService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 股票k线图
 * @Author: jeecg-boot
 * @Date:   2022-01-10
 * @Version: V1.0
 */
@Service
public class TbStockKServiceImpl extends ServiceImpl<TbStockKMapper, TbStockK> implements ITbStockKService {

    //发送请求方法
    @Autowired
    private JstlHttpUpload jstlHttpUpload;

    //保存list集合
    @Override
    public void saveList(String remark) throws ParseException {

        //进行数据处理
        boolean flag = remark.substring(0, 1).equals("6");

        String url = "";
        //判断是不是 600905
        if (flag){
            //是
            //判断是不是福耀玻璃
            if ("600660".equals(remark) || "600036".equals(remark)){
                url = "http://69.push2his.eastmoney.com/api/qt/stock/kline/get?cb=jQuery1124015040734427058466_1646058605150&secid=1."+remark+"&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&klt=101&fqt=0&end=20500101&lmt=120&_=1646058605255";

            }else if("600905".equals(remark)){
                url = "http://26.push2his.eastmoney.com/api/qt/stock/kline/get?cb=jQuery112406276904701364132_1647858959175&secid=1."+remark+"&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&klt=101&fqt=0&beg=0&end=20500101&smplmt=460&lmt=1000000&_=1647858959215";
            }else{
                url = "http://86.push2his.eastmoney.com/api/qt/stock/kline/get?cb=jQuery1124023670711975621095_1646227311848&secid=1."+remark+"&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&klt=101&fqt=0&end=20500101&lmt=120&_=1646227311939";
            }


        }else{
            //不是
            url = "http://78.push2his.eastmoney.com/api/qt/stock/kline/get?cb=jQuery112408843913876222274_1637667605639&secid=0."+remark+"&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&klt=101&fqt=0&end=20500101&lmt=120&_=1637667605682";
        }




        String a = jstlHttpUpload.setGetHangfei(url, null,null);
        int index = a.indexOf("(");//获取第一个"("的位置
        String str1 = a.substring(index+1);//不包含"("
        String str = str1.substring(0, str1.length() - 2);


        JSONObject jsonObject = JSON.parseObject(str);
        String data = jsonObject.getString("data");
        JSONObject jsonData = JSON.parseObject(data);
            JSONArray klines = jsonData.getJSONArray("klines");
            String code = jsonData.getString("code");
            List<TbStockK> list = new ArrayList<>();
            for (int i = 0; i < klines.size(); i++) {
                String kline = klines.getString(i);
                String[] split = kline.split(",");

                String dateTime =  split[0];//时间
                String opening =  split[1];//开盘
                String closing =  split[2];//收盘
                String hig =  split[3];//最高
                String lowest =  split[4];//最低
                String turnover =  split[5];//成交量
                String gmv =  split[6];//成交额
                String swing =  split[7];//振幅
                String price =  split[8];//涨跌幅
                String amount =  split[9];//涨跌额
                String tun =  split[10];//换手率

                TbStockK bean = new TbStockK();
                bean.setId(code+"_"+dateTime);
                bean.setStockId(code);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date parse = sdf.parse(dateTime);
                bean.setDateTime(parse);
                bean.setOpening( Double.parseDouble(opening));
                bean.setClosing(Double.parseDouble(closing));
                bean.setHig(Double.parseDouble(hig));
                bean.setLowest(Double.parseDouble(lowest));
                bean.setTurnover(Double.parseDouble(turnover));
                bean.setGmv(Double.parseDouble(gmv));
                bean.setSwing(Double.parseDouble(swing));
                bean.setPrice(price);
                bean.setAmount(amount);
                bean.setTun(Double.parseDouble(tun));
                bean.setRemark(kline);
                list.add(bean);
            }
            this.saveOrUpdateBatch(list);

    }
}
